package cn.jr.dao.impl;

import cn.jr.dao.IActiveDao;
import cn.jr.dao.IOrderProjectDao;
import cn.jr.pojo.Active;
import cn.jr.pojo.OrderProject;
import cn.jr.pojo.ShopRun;
import cn.jr.pojo.Wares;
import cn.jr.util.DBHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class OrderProjectDaoImpl implements IOrderProjectDao {
    DBHelper db=new DBHelper();
    IActiveDao iad=new ActiveDaoImpl();
    @Override
    public ArrayList<OrderProject>  selectOrderProject(String orderId) {
        /**
         * 根据订单编号查询商品详情
         * @param orderId 订单编号
         * @return 订单详情
         *
         */
        String sql="select * from torderporject where orderId=?";
        return db.dtQuery(sql, OrderProject.class,orderId);
    }

    @Override
    public OrderProject selectOrderProjectInOrderByWaresNumber(String orderId, String waresNumber) {
        /**
         * 根据订单编号和商品编号查询商品详情
         * @param orderId 订单编号
         * @param waresNumber 商品编号
         * @return 订单详情
         */
        String sql="select * from torderporject where orderId=? and waresNumber=?";
        ArrayList<OrderProject> list=db.dtQuery(sql, OrderProject.class,orderId,waresNumber);
        return list.size()>0?list.get(0):null;
    }
    public int deleteOrderProjectByOrderProjectId(OrderProject orderProject) {
        /**
         * 根据订单详情编号删除订单详情
         * @param orderProject 订单详情
         * @return 影响行数
         */
        String sql="delete from torderporject where orderProjectId=?";
        return db.dtUpdate(sql,orderProject.getOrderProjectId());
    }

    @Override
    public OrderProject selectOrderProjectEnd() {
        /**
         * 查询订单详情最后一条数据
         * @return 订单详情
         * @return 订单详情

         */
        String sql ="select * from torderporject";
        ArrayList list = db.dtQuery(sql,OrderProject.class);
        return (OrderProject)list.get(list.size()-1);
    }

    @Override
    public int insertOrderProjectInOrder(Wares ware, Double waresCount) {
        /**
         * 新增下一个客户的订单项表
         * @param orderId 订单编号  要加1
         * @param ware 商品
         * @param waresCount 商品数量
         * @return 影响行数
         */
        String sql="insert into torderporject values(?,?,?,?,?,?,?,?)";
        OrderProject orderProject=new OrderProject();
        int orderProjectId =Integer.parseInt(selectOrderProjectEnd().getOrderProjectId().substring(5))+1;
        orderProject.setOrderProjectId("proid"+orderProjectId);
        int orderId =Integer.parseInt(selectOrderProjectEnd().getOrderId().substring(2))+1;
        orderProject.setOrderId("id"+orderId);
        double douWaresAllPrice=ware.getWaresPrice()*waresCount;
        double orderProjectPrice=douWaresAllPrice*cxActiveMoney();
        Object[] objs={orderProject.getOrderProjectId(),orderProject.getOrderId(),ware.getWaresNumber(),ware.getWaresName(),ware.getWaresPrice(),waresCount,douWaresAllPrice,orderProjectPrice};
        return db.dtUpdate(sql,objs);
    }

    @Override
    public int insertOrderProject(Wares ware, Double waresCount) {
        /**
         * 新增该客户的订单项表
         * @param orderId 订单编号  不变
         * @param ware 商品
         * @param waresCount 商品数量
         * @return 影响行数
         * 如果该商品不在该客户订单项中，就添加新的订单项。
         * 如果该商品已经在该客户订单项中了，就修改订单项。
         *  1.如果该商品数量增加了，就修改商品数量和商品总价。
         *  2.如果该商品数量减少了，就修改商品数量和商品总价。
         */
        String sql="insert into torderporject values(?,?,?,?,?,?,?,?)";
        OrderProject orderProject=new OrderProject();
        int orderProjectId =Integer.parseInt(selectOrderProjectEnd().getOrderProjectId().substring(5))+1;
        orderProject.setOrderProjectId("proid"+orderProjectId);
        orderProject.setOrderId(selectOrderProjectEnd().getOrderId());
        ArrayList<OrderProject> list =selectOrderProject(selectOrderProjectEnd().getOrderId());
        for(OrderProject o:list){
            if(o.getWaresNumber().equals(ware.getWaresNumber())){
                return updateOrderProject(ware,waresCount,o.getWaresCount(),o.getOrderProjectId());
            }
        }
        double douWaresAllPrice=ware.getWaresPrice()*waresCount;
        double orderProjectPrice=douWaresAllPrice*cxActiveMoney();
        Object[] objs={orderProject.getOrderProjectId(),orderProject.getOrderId(),ware.getWaresNumber(),ware.getWaresName(),ware.getWaresPrice(),waresCount,douWaresAllPrice,orderProjectPrice};
        return db.dtUpdate(sql,objs);
    }
    public int updateOrderProjectInTuiHuo(Wares ware, Double waresCount,String orderId) {
        /**
         * 退款时修改订单
         * @param ware 商品
         * @param waresCount 商品数量
         * @param orderId 订单编号
         * @return 影响行数
         */
        String sql="insert into torderporject values(?,?,?,?,?,?,?,?)";
        OrderProject orderProject=new OrderProject();
        int orderProjectId =Integer.parseInt(selectOrderProjectEnd().getOrderProjectId().substring(5))+1;
        orderProject.setOrderProjectId("proid"+orderProjectId);
        orderProject.setOrderId(selectOrderProjectEnd().getOrderId());
        ArrayList<OrderProject> list =selectOrderProject(orderId);
        for(OrderProject o:list){
            if(o.getWaresNumber().equals(ware.getWaresNumber())){
                return updateOrderProject(ware,waresCount,o.getWaresCount(),o.getOrderProjectId());
            }
        }
        double douWaresAllPrice=ware.getWaresPrice()*waresCount;
        double orderProjectPrice=douWaresAllPrice*cxActiveMoney();
        Object[] objs={orderProject.getOrderProjectId(),orderProject.getOrderId(),ware.getWaresNumber(),ware.getWaresName(),ware.getWaresPrice(),waresCount,douWaresAllPrice,orderProjectPrice};
        return db.dtUpdate(sql,objs);
    }
    public double cxActiveMoney(){
        /**
         * 计算活动金额
         * @param orderProjectId 订单编号
         * @return 活动金额
         */
        ArrayList<Active> list=iad.selectByActive();
        Date now = new Date();
        // 定义格式模板（如：年-月-日 时:分:秒）
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date activeStartTime =null;
        Date activeEndTime =null;
        for(Active a:list){
            try {
                activeStartTime = sdf.parse(a.getActiveStartTime());
                activeEndTime = sdf.parse(a.getActiveEndTime());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            if(!now.before(activeStartTime) && !now.after(activeEndTime)){
                return a.getActiveMoney();
            }
        }
        return 1;
    }


    @Override
    public int updateOrderProject(Wares ware, Double waresCount,Double waresCount1,String orderProjectId) {
        /**
         * 添加的商品相同时，不新增，直接修改商品数量，修改订单详情
         * @param ware 商品
         * @param waresCount 商品数量
         * @param orderProjectId 订单详情编号
         * @return 影响行数
         */
        String sql="update torderporject set waresCount=?,waresAllPrice=?,orderProjectPrice=? where orderProjectId=?";
        double waresCountNew=waresCount+waresCount1;
        if(waresCountNew<0.00){
            return -1;
        }
        if(waresCountNew==0.00){
            return -2;
        }
        double douWaresAllPriceNew=ware.getWaresPrice()*waresCountNew;
        double orderProjectPrice=douWaresAllPriceNew*cxActiveMoney();
        Object[] objs={waresCountNew,douWaresAllPriceNew,orderProjectPrice,orderProjectId};
        return db.dtUpdate(sql,objs);
    }

    @Override
    public int deleteOrderProject(String waresNumber) {
        /**
         * 删除订单详情
         * @param waresNumber 商品编号
         * @return 影响行数
         */
        String sql="delete from torderporject where orderProjectId=?";
        ArrayList<OrderProject> list =selectOrderProject(selectOrderProjectEnd().getOrderId());
        for(OrderProject o:list){
            if(o.getWaresNumber().equals(waresNumber)){
                return db.dtUpdate(sql,o.getOrderProjectId());
            }
        }
        return 0;
    }

    @Override
    public ArrayList<OrderProject> selectOrderProjectByWaresNumber(String waresNumber) {
        /**
         * 查询订单详情
         * @param waresNumber 商品编号
         * @return 订单详情集合
         */
        String sql="select * from torderporject where waresNumber=?";
        return db.dtQuery(sql,OrderProject.class,waresNumber);
    }
}
